/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.vcapi;

import java.io.Serializable;
import java.util.Properties;

/**
 *
 * @author yura
 */
public class HProperties implements Serializable{

    private String jdbcDriverClassName;
    private String jdbcUrl;
    private String jdbcUsername;
    private String jdbcPassword;
    private String hibernateHbm2ddlAuto;
    private String hibernateShowSql;
    private String hibernateDialect;

    public HProperties() {
        this(new Properties());
    }

    /**
     *  <p>The properties must contain the next property:</p>
     * <table border=1>
     *  <tr><th>Property Name</th><th>Description</th><th>Default value</th></tr>
     *  <tr>
     *      <td>jdbc.driverClassName</td>
     *      <td>The JDBC driver class</td>
     *      <td>org.postgresql.Driver</td>
     *  </tr>
     *  <tr>
     *      <td><b><u>jdbc.url</u></b></td>
     *      <td>The JDBC URL to the database instance</td>
     *      <td></td>
     *  </tr>
     *  <tr>
     *      <td><b>jdbc.username</b></td>
     *      <td>Database username</td>
     *      <td></td>
     *  </tr>
     *  <tr>
     *      <td><b>jdbc.password</b></td>
     *      <td>Database password</td>
     *      <td></td>
     *  </tr>
     *
     *  <tr>
     *      <td>hibernate.hbm2ddl.auto</td>
     *      <td>Automatically creates, updates, or drops  database schema on
     *          startup and shut down.  There are three possible values: create,
     *          create-  drop, and update. Be careful with create-drop!</td>
     *      <td>validate</td>
     *  </tr>
     *  <tr>
     *      <td>hibernate.show_sql</td>
     *      <td>Logs the generated SQL commands</td>
     *      <td>false</td>
     *  </tr>
     *  <tr>
     *      <td>hibernate.dialect</td>
     *      <td>SQL dialect to use for Hibernate, varies by database. See
     *          section on SQL dialects.</td>
     *      <td>org.hibernate.dialect.PostgreSQLDialect</td>
     *  </tr>
     * </table>
     *
     * @param properties
     */
    public HProperties(Properties properties) {
        jdbcDriverClassName = properties.getProperty("jdbc.driverClassName", "org.postgresql.Driver");
        jdbcUrl = properties.getProperty("jdbc.url", "");
        jdbcUsername = properties.getProperty("jdbc.username", "");
        jdbcPassword = properties.getProperty("jdbc.password", "");

        hibernateHbm2ddlAuto = properties.getProperty("hibernate.hbm2ddl.auto", "validate");
        hibernateShowSql = properties.getProperty("hibernate.show_sql", "false");
        hibernateDialect = properties.getProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
    }

    public Properties toProperties(){
        Properties properties = new Properties();
        properties.setProperty("jdbc.driverClassName", jdbcDriverClassName);
        properties.setProperty("jdbc.url", jdbcUrl);
        properties.setProperty("jdbc.username", jdbcUsername);
        properties.setProperty("jdbc.password", jdbcPassword);

        properties.setProperty("hibernate.hbm2ddl.auto", hibernateHbm2ddlAuto);
        properties.setProperty("hibernate.show_sql", hibernateShowSql);
        properties.setProperty("hibernate.dialect", hibernateDialect);
        return properties;
    }

    public String getHibernateDialect() {
        return hibernateDialect;
    }

    public void setHibernateDialect(String hibernateDialect) {
        this.hibernateDialect = hibernateDialect;
    }

    public String getHibernateHbm2ddlAuto() {
        return hibernateHbm2ddlAuto;
    }

    public void setHibernateHbm2ddlAuto(String hibernateHbm2ddlAuto) {
        this.hibernateHbm2ddlAuto = hibernateHbm2ddlAuto;
    }

    public String getHibernateShowSql() {
        return hibernateShowSql;
    }

    public void setHibernateShowSql(String hibernateShowSql) {
        this.hibernateShowSql = hibernateShowSql;
    }

    public String getJdbcDriverClassName() {
        return jdbcDriverClassName;
    }

    public void setJdbcDriverClassName(String jdbcDriverClassName) {
        this.jdbcDriverClassName = jdbcDriverClassName;
    }

    public String getJdbcPassword() {
        return jdbcPassword;
    }

    public void setJdbcPassword(String jdbcPassword) {
        this.jdbcPassword = jdbcPassword;
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public String getJdbcUsername() {
        return jdbcUsername;
    }

    public void setJdbcUsername(String jdbcUsername) {
        this.jdbcUsername = jdbcUsername;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final HProperties other = (HProperties) obj;
        if ((this.jdbcDriverClassName == null) ? (other.jdbcDriverClassName != null) : !this.jdbcDriverClassName.equals(other.jdbcDriverClassName)) {
            return false;
        }
        if ((this.jdbcUrl == null) ? (other.jdbcUrl != null) : !this.jdbcUrl.equals(other.jdbcUrl)) {
            return false;
        }
        if ((this.jdbcUsername == null) ? (other.jdbcUsername != null) : !this.jdbcUsername.equals(other.jdbcUsername)) {
            return false;
        }
        if ((this.jdbcPassword == null) ? (other.jdbcPassword != null) : !this.jdbcPassword.equals(other.jdbcPassword)) {
            return false;
        }
        if ((this.hibernateHbm2ddlAuto == null) ? (other.hibernateHbm2ddlAuto != null) : !this.hibernateHbm2ddlAuto.equals(other.hibernateHbm2ddlAuto)) {
            return false;
        }
        if ((this.hibernateShowSql == null) ? (other.hibernateShowSql != null) : !this.hibernateShowSql.equals(other.hibernateShowSql)) {
            return false;
        }
        if ((this.hibernateDialect == null) ? (other.hibernateDialect != null) : !this.hibernateDialect.equals(other.hibernateDialect)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 79 * hash + (this.jdbcDriverClassName != null ? this.jdbcDriverClassName.hashCode() : 0);
        hash = 79 * hash + (this.jdbcUrl != null ? this.jdbcUrl.hashCode() : 0);
        hash = 79 * hash + (this.jdbcUsername != null ? this.jdbcUsername.hashCode() : 0);
        hash = 79 * hash + (this.jdbcPassword != null ? this.jdbcPassword.hashCode() : 0);
        hash = 79 * hash + (this.hibernateHbm2ddlAuto != null ? this.hibernateHbm2ddlAuto.hashCode() : 0);
        hash = 79 * hash + (this.hibernateShowSql != null ? this.hibernateShowSql.hashCode() : 0);
        hash = 79 * hash + (this.hibernateDialect != null ? this.hibernateDialect.hashCode() : 0);
        return hash;
    }
}
